/**
 * 
 */
package com.yidu.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.util.StringUtils;

import com.yidu.domain.Role;
import com.yidu.domain.vo.RoleVo;
import com.yidu.utils.Pages;

/**
 * @描述  角色Dao的实现类
 * @author 石嘉澄
 * @date 2019年9月25日
 */
public class RoleDaoImpl implements RoleDao {

	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	/**
	 * 新增
	 */
	public void add(Role role) {
		hibernateTemplate.save(role);
		
	}

	/**
	 * 删除
	 */
	public void delete(String roleId) {
		hibernateTemplate.delete(roleId);
		
	}

	/**
	 * 修改
	 */
	public void update(Role role) {
		hibernateTemplate.update(role);
		
	}

	/**
	 * 查询所有分页
	 */
	public List<Role> findAllPage(Role role, Pages page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
		if(!StringUtils.isEmpty(role.getRoleName())) {
			criteria.add(Restrictions.ilike("roleName", "%"+role.getRoleName()+"%"));
		}
		@SuppressWarnings("unchecked")
		List<Role> list = (List<Role>) hibernateTemplate.findByCriteria(criteria,page.getStartRows(),page.getLimit());
		return list;
	}

	/**
	 * 根据ID查询
	 */
	public Role findById(String roleId) {
		// TODO Auto-generated method stub
		return hibernateTemplate.get(Role.class, roleId);
	}

	/**
	 * 查询总行数
	 */
	public Long count(RoleVo vo) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
		criteria.setProjection(Projections.rowCount());
		if(!StringUtils.isEmpty(vo.getRoleName())) {
			criteria.add(Restrictions.ilike("roleName", "%"+vo.getRoleName()+"%"));
		}
		Long count = (Long) hibernateTemplate.findByCriteria(criteria).get(0);
		return count;
	}
	/**
	 * 查询全部不分页
	 */
	@Override
	public List<Role> findAll(Role role) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
		@SuppressWarnings("unchecked")
		List<Role> list = (List<Role>) hibernateTemplate.findByCriteria(criteria);
		return list;
	}

	@Override
	public void updateRolevalidity(String roleId, String roleValidity) {
		Role role=findById(roleId);
		role.setRoleValidity(roleValidity);
		update(role);
	}

	

}
